import java.util.*;

import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.*;
import utilities.event.*;
import utilities.utilities_components.PlainList;

public class ServiceRegController
extends Window{
	
	private Textbox name;
	private Doublebox price;
	private Textbox note;
	private Button add,del;
	private PlainList list;
	
	public void ServiceRegController(){
		list=new PlainList();
		list.setListener(new PlainList.SelectListener(){
			public void onFocus(Component comp){
				if(del != null && list.getSelectedIndex() >= 0)
					del.setDisabled(false);
				else del.setDisabled(true);
			}
		});
		this.addEventListener("onNotify",new ServiceListener());
	}
	
	public void initialize(){
		this.name=this.getFellow("INFO").getFellow("NAME");
		this.price=this.getFellow("INFO").getFellow("PRICE");
		this.note=this.getFellow("INFO").getFellow("NOTE");
		this.add=this.getFellow("INFO").getFellow("ADD");
		this.del=this.getFellow("INFO").getFellow("DEL");
		String[] head={"ID","名称","价格","备注"};
		list.setHead(head);
		list.setParent(this.getFellow("LIST"));
		this.getPage().setAttribute("ACTION","FLUSH");
		Events.sendEvent(new Event("onNotify",this));
		list.update(this.getPage().getAttribute("COLDATA"));
	}
	
	public void nameValidate(){
		String str=this.name.getValue();
		if(str == null || str.equals(""))
			throw new WrongValueException(name,"名字不能为空");
		if(str.length() >= 30)
			throw new WrongValueException(name,"名字长度必须小于30");
		this.getPage().setAttribute("NAME",str);
	}
	
	public void noteValidate(){
		String str=this.note.getText();
		if(str == null || str.length() >= 100)
			throw new WrongValueException(note,"备注长度必须小于100");
		this.getPage().setAttribute("NOTE",str);
	}
	
	public void priceValidate(){
		String str=this.price.getText();
		if(str == null || str.equals(""))
			throw new WrongValueException(price,"价格不能为空");
		if(price.getValue() <= 0)
			throw new WrongValueException(price,"价格不能小于等于0");
		this.getPage().setAttribute("PRICE",price.getValue());
	}
	
	public void onAdd(){
		nameValidate();
		priceValidate();
		noteValidate();
		this.getPage().setAttribute("ACTION","ADD");
		Events.sendEvent(new Event("onNotify",this));
		list.update(this.getPage().getAttribute("COLDATA"));
	}
	
	public void onDel(){
		if((list.getSelectedIndex() < 0)){
			try{new Messagebox().show("请输入要删除的数据");}catch(Exception e){}
			return;
		}
		this.getPage().setAttribute("ID",
			((Listcell)list.getColDate(list.getSelectedIndex()).get(0)).getLabel());
		this.getPage().setAttribute("ACTION","DEL");
		Events.sendEvent(new Event("onNotify",this));
		list.update(this.getPage().getAttribute("COLDATA"));
	}
}